/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://iv-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - Handlers/taxi.nut                                  *
*                                                                               *
*  The taxi handler is located here                                             *
*                                                                               *
*  @author Wesley (wesley@sa-mp.nl)                                             *
*                                                                               *
********************************************************************************/

class TaxiService
{
    constructor ()
    {
        loadXmlFile ();
        
        if (!_xmlFile.lastError ())
        {
            loadSettings ();
            loadLocations ();
            _enabled = true;
        }
    }
    
    function loadXmlFile ()
    {
        _xmlFile = xml ("taxi.xml");
    }
    
    function loadSettings ()
    {
        _xmlFile.nodeFind ("settings");
        _xmlFile.nodeFind ("price");
        _taxiSettings ["price"] <- _xmlFile.nodeContent ();
        _xmlFile.nodeRoot ();
    }
    
    function loadLocations ()
    {
        _xmlFile.nodeFind ("locations");
        _xmlFile.nodeFirstChild ();
        
        do
        {
            local name, x, y, z, heading;
                
            name = _xmlFile.nodeAttribute ("name");
            x = _xmlFile.nodeAttribute ("x").tofloat();
            y = _xmlFile.nodeAttribute ("y").tofloat();
            z = _xmlFile.nodeAttribute ("z").tofloat();
            heading = _xmlFile.nodeAttribute ("heading").tofloat();
            
            _taxiLocations.append ([name, x, y, z, heading]);
        } 
        while (_xmlFile.nodeNext ());
    }
    
    function isEnabled ()
    {
        return _enabled;
    }
    
    function getLocations ()
    {
        return _taxiLocations;
    }
    
    function isValidLocation (locationid)
    {
        return (locationid in _taxiLocations);
    }
    
    function getLocationName (locationid)
    {
        return _taxiLocations [locationid] [0];
    }
    
    function taxiPlayerToLocation (playerid, locationid)
    {
        setPlayerCoordinates (playerid, _taxiLocations [locationid] [1], _taxiLocations [locationid] [2], _taxiLocations [locationid] [3]); 
        setPlayerHeading (playerid, _taxiLocations [locationid] [4]);
        setCameraBehindPlayer (playerid);
    }
    
    _enabled = false;
    _xmlFile = false;
    _taxiSettings = {};
    _taxiLocations = [];
}

local taxiHandler = TaxiService ();
local cmdHandler = CommandHandler ();

local function cmdLocations (playerid, cmdWords, cmdStr)
{
    if (!taxiHandler.isEnabled ())
        return;
    
    sendPlayerMessage (playerid, "The following taxi locations are available:", 0xFF9900FF, true);

    local nextMessage = "";

    foreach (locationid, location in taxiHandler.getLocations())
    {
        if (nextMessage != "")
        {
            nextMessage += "   " + locationid + ": " + location [0];
        } else
        {
            nextMessage += locationid + ": " + location [0];
        }

        if (nextMessage.len () >= 75)
        {
            sendPlayerMessage (playerid, nextMessage, 0xFFFFFF00, true);
            nextMessage = "";
        }
    }

    if (nextMessage != "")
        sendPlayerMessage (playerid, nextMessage, 0xFFFFFF00, true);
}
cmdHandler.registerCommand ("/locations", Command (cmdLocations), COMMAND_LEVEL_PLAYER);

local function cmdTaxi (playerid, cmdWords, cmdStr)
{
    if (!taxiHandler.isEnabled ())
        return;
    
    if (cmdWords.len () < 2 || !taxiHandler.isValidLocation (cmdWords[1].tointeger()))
    {
        sendPlayerUsageMessage (playerid, "/taxi [Location]");
        sendPlayerErrorMessage (playerid, "See /locations for taxi locations.")
        return;
    }
    
    if (isPlayerInAnyVehicle (playerid))
    {
        sendPlayerErrorMessage (playerid, "You can't get into a taxi while you're in a vehicle!");
        return;
    }
    
    local locationid = cmdWords[1].tointeger();
    
    taxiHandler.taxiPlayerToLocation (playerid, locationid);
    sendPlayerMessage (playerid, "You have arrived at " + taxiHandler.getLocationName (locationid) + ".", 0xFF9900FF, true);    
}
cmdHandler.registerCommand ("/taxi", Command (cmdTaxi), COMMAND_LEVEL_PLAYER);